package com.bayss.core.service.address;



import com.bayss.core.entity.address.AddressFixedInfo;
import com.bayss.core.entity.address.mapper.AddressFixedInfoMapper;
import com.bayss.core.entity.station.Station;
import com.bayss.core.entity.station.mapper.StationMapper;
import com.bayss.core.entity.user.AddressInfo;
import com.bayss.core.entity.user.AddressInfoExample;
import com.bayss.core.entity.user.UserInfo;
import com.bayss.core.entity.user.mapper.AddressInfoMapper;
import com.bayss.core.util.OrderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AddressService {

    private final AddressInfoMapper addressInfoMapper;

    private final StationMapper stationMapper;

    private final AddressFixedInfoMapper addressFixedInfoMapper;

    @Autowired
    public AddressService(AddressInfoMapper addressInfoMapper, StationMapper stationMapper, AddressFixedInfoMapper addressFixedInfoMapper) {
        this.addressInfoMapper = addressInfoMapper;
        this.stationMapper = stationMapper;
        this.addressFixedInfoMapper = addressFixedInfoMapper;
    }

    /**
     * 创建地址
     */
    public boolean save(AddressInfo addressInfo){
        Long addressId = addressInfo.getId();
        int defaultAddr = addressInfo.getDefaultAddr();
        if (defaultAddr == 1) {
            AddressInfoExample addressInfoExample = new AddressInfoExample();
            addressInfoExample.createCriteria().andUidEqualTo(addressInfo.getUid());
            AddressInfo address = new AddressInfo();
            address.setDefaultAddr(0);
            addressInfoMapper.updateByExampleSelective(address, addressInfoExample);
        }

        if (addressId == null || addressId == 0) {
            return addressInfoMapper.insertSelective(addressInfo) > 0;
        } else {
            return addressInfoMapper.updateByPrimaryKeySelective(addressInfo) > 0;
        }
    }

    /**
     * 计算距离
     */
    public double costM(Long addressId, Long stationId){
        AddressInfo addressInfo = get(addressId);
        Station station = stationMapper.selectByPrimaryKey(stationId);
        return OrderUtils.costM(Double.parseDouble(addressInfo.getLatitude()),
                Double.parseDouble(addressInfo.getLongitude()),
                Double.parseDouble(station.getLatitude()),
                Double.parseDouble(station.getLongitude()));
    }

    /**
     * 获取地址
     */
    public List<AddressInfo> getByUserId(long uid){
        AddressInfoExample addressInfoExample = new AddressInfoExample();
        AddressInfoExample.Criteria criteria = addressInfoExample.createCriteria();
        criteria.andUidEqualTo(uid);

        List<AddressInfo> addressInfos = addressInfoMapper.selectByExample(addressInfoExample);
        for (AddressInfo addressInfo : addressInfos) {
            if (addressInfo.getTab() == 4) {
                AddressFixedInfo addressFixedInfo = addressFixedInfoMapper.selectByPrimaryKey(addressInfo.getFixAddrId());
                addressFixedInfo.setStation(stationMapper.selectByPrimaryKey(addressFixedInfo.getStationId()));
                addressInfo.setAddressFixedInfo(addressFixedInfo);
            }
        }
        return addressInfos;
    }

    /**
     * 设置默认地址
     */
    public void setDefaultAddr(UserInfo userInfo,Long id) {
        // 取消已有默认
        AddressInfo updateRecord = new AddressInfo();
        updateRecord.setDefaultAddr(0);
        AddressInfoExample example = new AddressInfoExample();
        example.createCriteria().andDefaultAddrEqualTo(1).andUidEqualTo(userInfo.getId());
        addressInfoMapper.updateByExampleSelective(updateRecord, example);

        // 设置新默认
        AddressInfo record = new AddressInfo();
        record.setDefaultAddr(1);
        record.setId(id);
        record.setUpdateTime(new Date());
        addressInfoMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 删除地址
     */
    public boolean delAddr(long addrId){
        return addressInfoMapper.deleteByPrimaryKey(addrId) > 0;
    }

    public AddressInfo get(Long id){
        if (id == null) {
            return null;
        }
        return addressInfoMapper.selectByPrimaryKey(id);
    }


}
